package com.glc.hms.core.compress.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.xerial.snappy.Snappy;

import java.io.IOException;

/**
 * Snappy压缩算法
 *
 * @author Lin
 * @time 2019-12-04 22:16
 */
@Slf4j
public class SnappyCompressAlgorithm implements CompressAlgorithm {

    @Override
    public byte[] compress(byte[] data) {
        if (data == null || data.length == 0) {
            return new byte[0];
        }
        try {
            return Snappy.compress(data);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return new byte[0];
    }

    @Override
    public byte[] decompress(byte[] data) {
        if (data == null || data.length == 0) {
            return null;
        }
        try {
            return Snappy.uncompress(data);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }
}
